#ifndef _Q6_H 
#define _Q6_H 
#include <iostream>
#include <fstream>
#include <map>
#include <cstring>
using namespace std ;
struct CompByKey
{
bool operator() (const string &s1 ,const string &s2)
{
    return  s1.compare(s2);
}
};
class Dict
{
private:
    map<string,int> dict ;
public:
    void readFromFile(const char * path);
    void readFromString(const string& _str);
    friend ostream& operator<< (ostream & stm ,Dict d);
};
ostream &operator<< (ostream & stm ,Dict d)
{
    for (map<string,int>::iterator ite = d.dict.begin();ite!=d.dict.end();++ite)
    {
        stm<<(*ite).first<<":"<<(*ite).second<<endl;
    }
    return stm;
}
void Dict::readFromString(const string& _str)
{
    size_t ptr=0,ptrprev=0;
    size_t size = _str.size();
    if(_str=="")
    return ;
    while (ptr !=string::npos)
    {
        
        //TODO 是否可以辨别所有的字符，考虑直接判断ascii范围
        ptr =_str.find(" ",ptrprev);
        string substr=_str.substr(ptrprev,ptr-ptrprev);
        map<string,int>::iterator ite =dict.find(substr);
        if(ite==dict.end())
        {
            dict.insert(make_pair(substr,1));
        }
        else 
        {
            (*ite).second++;
        }
        ptrprev=ptr+1;
    }
}
void Dict::readFromFile(const char * path)

{
    ifstream infile(path,ios::in);
    char buffer[500];
    while(infile.good())
    {
        infile.getline(buffer,500);
        string str(buffer);
        readFromString(str);
    }
}

#endif	// _Q6_H


// TODO q6没写完